package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.service.ReportService;
import com.itheima.vo.BusinessReportDataVO;
import com.itheima.vo.MemberReportVO;
import com.itheima.vo.SetmealReportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController {

    @Autowired
    private ReportService reportService;

    /**
     * 获取会员数量统计数据
     * @return
     */
    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        log.info("获取会员数量统计数据");
        try {
            MemberReportVO memberReportVO = reportService.getMemberReport();
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, memberReportVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL, null);
        }
    }


    /**
     * 获取套餐占比统计数据
     * @return
     */
    @GetMapping("/getSetmealReport")
    public Result getSetmealReport(){
        log.info("获取套餐占比统计数据");
        try {
            SetmealReportVO setmealReportVO = reportService.getSetmealReport();
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, setmealReportVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL, null);
        }
    }

    /**
     * 获取运营统计数据
     * @return
     */
    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        log.info("获取运营统计数据");
        try {
            BusinessReportDataVO businessReportDataVO = reportService.getBusinessReportData();
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, businessReportDataVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL, null);
        }
    }

    /**
     * 导出运营数据到Excel并提供客户端下载
     * @return
     */
    @GetMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response){
        log.info("导出运营数据到Excel并提供客户端下载");
        try {
            reportService.exportBusinessReport(response);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "导出运营数据到Excel并提供客户端下载失败", null);
        }
    }
}
